<template>
  <div style="display: inline-block;margin-right:10px;">
    <slot name="before" />
    <!--未启动和流程终止状态下可以启动流程-->
    <el-button
      v-if="editBtn"
      v-hasPermi="editHasPermi"
      size="small"
      type="text"
      :style="editBtnStyle"
      icon="el-icon-video-play"
      @click="updateHandle()"
    >{{ $t("启动流程") }}
    </el-button>
    <!--带审批状态下可以终止流程-->
    <el-button
      v-if="stopBtn"
      v-hasPermi="editHasPermi"
      size="small"
      type="text"
      :style="stopBtnStyle"
      icon="el-icon-video-pause"
      @click="stopProcess(busData)"
    >{{ $t("终止流程") }}</el-button>
    <!--未启动和流程终止状态下可以修改数据-->
    <!--<el-button-->
    <!--v-if="editBtn"-->
    <!--v-hasPermi="editHasPermi"-->
    <!--size="small"-->
    <!--type="text"-->
    <!--icon="el-icon-edit"-->
    <!--@click="updateHandle()"-->
    <!--&gt;修改</el-button>-->
    <!--未启动和流程终止状态下可以删除数据-->
    <el-button
      v-if="editBtn"
      v-hasPermi="removeHasPermi"
      size="small"
      type="text"
      :style="delBtnStyle"
      icon="el-icon-delete"
      @click="deleteHandle()"
    >{{ $t("删除") }}</el-button>

    <el-button
      v-if="$store.state.user.userId === '1' && !editBtn && !stopBtn && !isEmpty(dataTable) && busData.actStatus === 'W'"
      size="small"
      type="text"
      icon="el-icon-circle-close"
      @click="processDiscard"
    >{{ $t('流程作废') }}</el-button>

    <!--    <el-button v-if="!stopBtn && !editBtn && otherBtn" type="text" size="small" disabled icon="el-icon-lock">{{ $t("无权限") }}</el-button>-->
  </div>
</template>

<script>
  import { processDiscard } from '@/api/act/process/actProcess'

  export default {
    name: 'ListBtn',
    props: {
      // 业务数据
      busData: {
        type: Object,
        required: true
      },
      // 申请人或创建人
      apply: {
        type: String,
        required: false,
        default: undefined
      },
      // 权限标签
      hasPermi: {
        type: String,
        default: undefined
      },
      // 启动流程 style
      editBtnStyle: {
        type: String,
        required: false,
        default: ''
      },
      // 终止流程 style
      stopBtnStyle: {
        type: String,
        required: false,
        default: ''
      },
      // 删除按钮 style
      delBtnStyle: {
        type: String,
        required: false,
        default: ''
      },
      // 作废流程按钮
      abandonmentBtn: {
        type: Boolean,
        default: false
      },
      // 流程标识
      dataTable: {
        type: String,
        default: ''
      },
      otherBtn: { // 当有其他操作按钮，不用展示无权限按钮
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        // 当前用户ID
        userId: this.$store.state.user.userId,
        // 删除权限表单
        removeHasPermi: this.hasPermi ? [this.hasPermi + ':remove'] : [],
        // 修改权限标签
        editHasPermi: this.hasPermi ? [this.hasPermi + ':edit'] : []
      }
    },
     computed: {
      // 修改，删除，启动流程按照是否展示
       editBtn: function() {
         if (this.busData.actStatus !== 'D' && this.busData.actStatus !== 'W') {
           return true
         } else {
           return false
         }
       },
       // 终止流程按钮是否展示
       stopBtn: function() {
         if (this.busData.actStatus === 'D' && ((this.apply !== undefined ? this.apply : this.busData.createBy.id) === this.userId)) {
           return true
         } else {
           return false
         }
       }
    },
    created() {
    },
    methods: {
      // 修改方法
      updateHandle: function() {
        if (this.$parent.$parent.$parent.$parent.updateHandle !== undefined) {
          this.$parent.$parent.$parent.$parent.updateHandle(this.busData)
        } else {
          this.$parent.$parent.$parent.updateHandle(this.busData)
        }
      },
      deleteHandle: function() {
        if (this.$parent.$parent.$parent.$parent.deleteHandle !== undefined) {
          this.$parent.$parent.$parent.$parent.deleteHandle(this.busData)
        } else if (this.$parent.$parent.$parent.deleteHandle !== undefined) {
          this.$parent.$parent.$parent.deleteHandle(this.busData)
        } else {
          this.$parent.$parent.$parent.updateHandle(this.busData)
        }
      },
      getList: function() {
        if (this.$parent.$parent.$parent.$parent.getList !== undefined) {
          this.$parent.$parent.$parent.$parent.getList()
        } else {
          this.$parent.$parent.$parent.getList()
        }
      },
      /**
       * 废弃流程
       */
      processDiscard() {
        var _this = this
        this.$confirm(this.$t('确定要作废该流程?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          processDiscard(_this.dataTable, _this.busData.id, _this.busData.actId).then(response => {
            if (response.code === 200) {
              _this.msgSuccess(_this.$t(response.msg))
              _this.getList()
            } else {
              _this.msgError(_this.$t(response.msg))
            }
          })
        }).catch(() => {
        })
      },
      /**
       * 停止流程
       * 作用: 终止流程走完后执行
       * 使用: 父页面添加 stopProcessCurrent 方法.
       */
      stopProcessMethod(that, actId, dataId) {
        if (typeof that.$parent.$parent.$parent.stopProcessCurrent === 'function') {
          that.$parent.$parent.$parent.stopProcessCurrent(that, actId, dataId)
        } else if (typeof that.$parent.$parent.$parent.$parent.stopProcessCurrent === 'function') {
          that.$parent.$parent.$parent.$parent.stopProcessCurrent(that, actId, dataId)
        }
      }
    }
  }
</script>
